<?php
/**
 * Tozi controller e syzdaden specialno za setFlash supbshteniq s redirect pri vklu4eno cache-rane
 * 
 * Za da nqma problem s cache-raneto, t.e. suobshteniqta da ne ostavat v cache-ranata stranica ne trqbva
 * da se redirectvat setFlash+redirect() kym cache-rani stranici
 * 
 * Za tova se polzva tozi controller i negoviq metod redirect(), t.e. suobshteniqta shte se poqvqvat
 * v otdelna stranica na action-a redirect() i sled tova redirect() shte prenaso4va ve4e kym
 * cache-ranata (moje da ne e) stranica
 */
class SystemRedirectsController extends AppController {
	
	var $name = 'SystemRedirects';
	
	/**
	 * Tyk zadavame spisaka na action-ite s publichen dostyp (action-ite bez authorizaciq)
	 */
	function __initPublicActions() {
		$this->Auth->allow(array(
			'init_db',
			'system_redirect'
		));
	}
	
	function beforeFilter() {
		parent::beforeFilter();
		
		$this->__initPublicActions();
    }
	
    function init_db() {
    	$this->loadModel('Group');
    	
    	$group =& $this->Group;
    	
    	//------------- root [id=1]
    	$group->id = 1;
    	
    	$this->Acl->allow($group, 'controllers');
    	//------------/ root [id=1]
    	
    	//------------- user [id=2]
    	$group->id = 2;
    	
    	$this->Acl->allow($group, 'controllers/SystemRedirects/system_redirect');
    	//------------/ user [id=2]
    	
    	echo "all done";
    	exit;
    }
    
    /**
     * Sluji za redirect nai-ve4e kym cache-rani stranici sled kato v tozi action pakaje setFlash-a
     * 
     * Ako v dadeniq action imame nqkolko redirect vyzmojnosti naprimer v __authConfig() na app_controller.php
     * togava izpolzvame $redirectName za da izpolzvame unikalnata sesiina promenliva
     * Format:
     * systemRedirectUrl__name_of_redirect_1
     * systemRedirectUrl__login
     * systemRedirectUrl__logout
     * 
     * 
     * @param unknown_type $redirectName
     */
    function system_redirect($redirectName = null) {
    	if(!empty($redirectName)) {
    		if($this->Session->check('systemRedirectUrl__'.$redirectName)) {
    			$systemRedirectUrl = $this->Session->read('systemRedirectUrl__'.$redirectName);
    		} else {
    			$systemRedirectUrl = '/';
    		}
    	} else {
    	    if($this->Session->check('systemRedirectUrl')) {
    			$systemRedirectUrl = $this->Session->read('systemRedirectUrl');
    		} else {
    			$systemRedirectUrl = '/';
    		}
    	}
    	
    	$jsSystemRedirectUrl = Router::url($systemRedirectUrl, true);
    	
    	$this->set('systemRedirectUrl', $systemRedirectUrl);
    	$this->set('jsSystemRedirectUrl', $jsSystemRedirectUrl);
    }
	
}
?>